package 力扣._49_字母异位词分组;

import java.util.*;

class Solution2 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            // 将单词字母排序后作为主键
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            //取出该主键对应的列表，若不存在则为该主键创造列表
            List<String> list = map.getOrDefault(key, new ArrayList<>());
            //将该单词添加到列表中
            list.add(str);
            //更新map
            map.put(key,list);
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {

        String[] strs = new String[2];
        strs[0] = "ac";
        strs[1] = "c";
//        strs[2] = "";
        List<List<String>> lists = new Solution2().groupAnagrams(strs);
        System.out.printf(lists.toString());
    }
}